<?php
//error_reporting(E_ALL);
//ini_set("display_errors", 1);

require_once (dirname(__FILE__).DIRECTORY_SEPARATOR."dbconnect.php");
require_once (dirname(__FILE__).DIRECTORY_SEPARATOR."class.gaparse.php");

if (!function_exists('apache_request_headers')) {
    function apache_request_headers() {
        $arh = array();
        $rx_http = '/\AHTTP_/';
        foreach ($_SERVER as $key => $val) {
            if (preg_match($rx_http, $key)) {
                $arh_key = preg_replace($rx_http, '', $key);
                $rx_matches = array();
                // do some nasty string manipulations to restore the original letter case
                // this should work in most cases
                $rx_matches = explode('_', $arh_key);
                if (count($rx_matches) > 0 and strlen($arh_key) > 2) {
                    foreach ($rx_matches as $ak_key => $ak_val)
                        $rx_matches[$ak_key] = ucfirst($ak_val);
                    $arh_key = implode('-', $rx_matches);
                }
                $arh[$arh_key] = $val;
            }
        }
        return ($arh);
    }

}

$headers = apache_request_headers();
$currentHost = str_ireplace('www.', '', $headers['HOST']);
if (strlen($currentHost) == 0) {
    $currentHost = 'localhost';
}

if(isset($_REQUEST['domain'])){
    $currentHost = $_REQUEST['domain'];
}

$tid = "mysqlerror_cantconnect";

if ($canDirectConnectToDB) {
    // Get incoming ad campaign data
    $aux = new GA_Parse($_COOKIE);
    $trackingInfo = array();
    // Get data from url first. If not set get data from cookie
    if (isset($_REQUEST['utm_source'])) {
        $trackingInfo['source'] = $_REQUEST['utm_source'];
    } else {
        $trackingInfo['source'] = $aux -> campaign_source;
        if ($trackingInfo['source'] == '(direct)') {
            $trackingInfo['source'] = 'direct';
        }
    }
    if (isset($_REQUEST['utm_medium'])) {
        $trackingInfo['medium'] = $_REQUEST['utm_medium'];
    } else {
        $trackingInfo['medium'] = $aux -> campaign_medium;
        if ($trackingInfo['medium'] == '(none)') {
            $trackingInfo['medium'] = '';
        }
    }
    if (isset($_REQUEST['utm_term'])) {
        $trackingInfo['term'] = $_REQUEST['utm_term'];
    } else {
        $trackingInfo['term'] = $aux -> campaign_term;
    }
    if (isset($_REQUEST['utm_content'])) {
        $trackingInfo['content'] = $_REQUEST['utm_content'];
    } else {
        $trackingInfo['content'] = $aux -> campaign_content;
    }
    if (isset($_REQUEST['utm_campaign'])) {
        $trackingInfo['campaign'] = $_REQUEST['utm_campaign'];
    } else {
        $trackingInfo['campaign'] = $aux -> campaign_name;
        if ($trackingInfo['campaign'] == '(direct)') {
            $trackingInfo['campaign'] = 'direct';
        }
    }
    $trackingInfo['affiliate_id'] = aiaffiliateid;
    $trackingInfo['created_at'] = 'NOW()';

       if (strlen($trackingInfo['source']) > 0) {
        // Insert row into database and use primary key as tracking ID.
        $query = "INSERT INTO tracking (source,domain,campaign,medium,content,term,affiliate_id,created_at) VALUES 
            (
            '" . $trackingInfo['source'] . "',
            '" . $currentHost . "',
            '" . $trackingInfo['campaign'] . "',
            '" . $trackingInfo['medium'] . "',
            '" . $trackingInfo['content'] . "',
            '" . $trackingInfo['term'] . "',
            " . $trackingInfo['affiliate_id'] . ",
            " . $trackingInfo['created_at'] . ")";
        mysql_query($query, $aicon);
        if (mysql_error($aicon)) {
            //return "Query Error (" . mysql_errno ( $aicon ) . ") : " . mysql_error ( $aicon );
            $tid = 'mysqlerror';
        } else {
            $tid = mysql_insert_id($aicon);
        }
} else {
    $urlQuery =  "http://moneybackreviews.com/aia/includes/getaitrackingcode.php?domain=".$currentHost."&";
    foreach($_REQUEST as $index=>$value){
        $urlQuery.=$index."=".$value."&";
    }
    $tid = file_get_contents($urlQuery);
}
}else{
    $tid = 'organic';
}

echo $tid;
?>